#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :test.py
# @Time      :2023/7/14 
# @Author    :CL
# @email     :1037654919@qq.com
import time


def get_page(kw = 'concept car',page=1):
    import requests

    headers = {
        "authority": "unsplash.com",
        "accept": "*/*",
        "accept-language": "en-US",
        "referer": "https://unsplash.com/s/photos/concept-car",
        "sec-ch-ua": "\"Not.A/Brand\";v=\"8\", \"Chromium\";v=\"114\", \"Microsoft Edge\";v=\"114\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Linux\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.58"
    }
    cookies = {
        "xp-promote-plus-photos": "control",
        "xp-search-synonym": "experiment",
        "require_cookie_consent": "false",
        "_gid": "GA1.2.1312468259.1689316988",
        "_sp_ses.0295": "*",
        "_ga": "GA1.2.394609605.1689316988",
        "_ga_21SLH4J369": "GS1.1.1689316988.1.1.1689317151.0.0.0",
        "azk-ss": "true",
        "_sp_id.0295": "ef319179-ac99-4e45-8959-c3299610c896.1689316990.1.1689317261..527762ac-dd49-47f7-bb64-d26cef807a67..f74e9f61-abb2-4ecc-af1a-a38dca6b5989.1689316989637.45"
    }
    url = "https://unsplash.com/napi/search/photos"
    params = {
        "query": kw,
        "per_page": "20",
        "page": page,
        "xp": "search-synonym:experiment"
    }
    for __ in range(5):
        try:
            response = requests.get(url, headers=headers, cookies=cookies, params=params)

            # print(response.text)
            # print(response)
            if response.status_code ==200:
                return response
            time.sleep(3)
        except:
            pass
    return None


if __name__ =="__main__":
    response = get_page()
    json_data  = response.json()
    results = json_data['results']
    result = results[0]
    id  =result['id']
    alt_description  =result['alt_description']
    urls  =result['urls']['full']
    tags_preview = result['tags_preview']
    tags=[]
    for tag_preview in tags_preview:
        tags.append(tag_preview['title'])


    print(id,alt_description,urls,tags)